<template>
	<view class="content">
		<view v-if="!changeSuccess">
			<view class="uni-form-item uni-column">
				<input v-model="phone" type="tel" class="uni-input" name="" placeholder="请输入手机号" />
			</view>
			<view class="uni-form-item uni-column column-with-btn">
				<input v-model="smscode" type="number" class="uni-input" name="" placeholder="请输入验证码" />
				<button :class="{active : !disableCodeBtn}" :disabled="disableCodeBtn" @tap="sendCode">{{codeBtn.text}}</button>
			</view>
			<view class="uni-form-item uni-column">
				<input v-model="newPwd" type="password" class="uni-input" name="" placeholder="请输入新密码" />
			</view>
			<view class="uni-form-item uni-column">
				<input v-model="newPwdConfirm" type="password" class="uni-input" name="" placeholder="请再次输入新密码" />
			</view>
			<button @tap="forgetPassword" type="primary">提交</button>
		</view>
		<view v-if="changeSuccess">
			<view class="text-reset">重置成功，新密码已生效！</view>
			<button type="primary" @tap="gotoLogin">立即登陆</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				smscode:'',
				newPwd:'',
				newPwdConfirm:'',
				seconds: 60,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				},
				changeSuccess: false
			}
		},
		onLoad() {

		},
		methods: {
			sendCode: function () {
				var me = this;
				if (me.phone.length == 0) {
					me.$api.msg('请输入手机号');
					return;
				}
				if (me.phone.length != 11) {
					me.$api.msg('请输入合法手机号');
					return;
				}
				uni.showLoading({
					title:''
				})
				uni.request({
					url: me.serverUrl + '/core/api/v1/common/sendSms/changePayPwd',
					method: "POST",
					data: {
						'phoneNumber': me.phone
					},
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						me.codeBtn.waitingCode = true;
						me.codeBtn.count = me.seconds;
						me.codeBtn.text = me.codeBtn.count + 's';
				
						let countdown = setInterval(() => {
							me.codeBtn.count--;
							me.codeBtn.text = me.codeBtn.count + 's';
							if (me.codeBtn.count < 0) {
								clearInterval(countdown);
								me.codeBtn.text = '重新发送';
								me.codeBtn.waitingCode = false;
							}
						}, 1000);
					},
					complete: (error) => {
						uni.hideLoading();
						console.log(error)
					}
				})
			},
			forgetPassword: function () {
				var me = this;
				if (me.phone.length == 0) {
					me.$api.msg('请输入手机号');
					return;
				}
				if (me.phone.length != 11) {
					me.$api.msg('请输入合法手机号');
					return;
				}
				if (me.smscode.length == 0) {
					me.$api.msg('请输入验证码');
					return;
				}
				if (me.newPwd.length == 0) {
					me.$api.msg('请输入密码');
					return;
				}
				if (me.newPwd != me.newPwdConfirm) {
					me.$api.msg('请保证登陆密码一致');
					return;
				}
				uni.showLoading({
					title:''
				})
				uni.request({
					url: me.serverUrl + '/core/api/v1/hy/passport/forget_pay_pwd',
					method: "PUT",
					data: {
						'newPwd': me.newPwd,
						'newPwdConfirm': me.newPwdConfirm,
						'phone': me.phone,
						'smscode': me.smscode
					},
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.data.code == 200) {
							me.changeSuccess = true;
						}
					},
					complete: (error) => {
						uni.hideLoading()
						console.log(error)
					}
				})
			},
			gotoLogin: function () {
				uni.navigateBack({
					
				})
			}
		},
		computed: {
			disableCodeBtn: function (){
				return this.codeBtn.waitingCode;
			} 
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #12B3F9;
	.content{
		padding: 100upx;
	}
	.uni-form-item{
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;
		.uni-input{
			font-size: 30upx;
			padding: 7px 0;
		}
	}
	.column-with-btn{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		button{
			font-size: 24upx;
			margin: 0;
			width: 180upx;
			text-align: center;
			&:after{
				border: none
			}
			&.active{
				background-color: $color-primary;
				color: $uni-text-color-inverse;
			}
		}
	}
	.img-captcha{
		width: 150upx;
		height: 60upx;
	}
	button[type="primary"]{
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}
	.text-reset{
		text-align: center;
		margin-bottom: 100upx;
		font-size: 36upx;
	}
</style>
